package com.raojunxue.company.util;

import com.google.appengine.api.xmpp.JID;
import com.google.appengine.api.xmpp.Message;
import com.google.appengine.api.xmpp.MessageBuilder;
import com.google.appengine.api.xmpp.SendResponse;
import com.google.appengine.api.xmpp.XMPPService;
import com.google.appengine.api.xmpp.XMPPServiceFactory;

public class XMPPUtil {

	public static void sendMessage(String username, String content) {
		 JID jid = new JID(username);
	        Message msg = new MessageBuilder()
	            .withRecipientJids(jid)
	            .withBody(content)
	            .build();
	        boolean messageSent = false;
	        XMPPService xmpp = XMPPServiceFactory.getXMPPService();
	        if (xmpp.getPresence(jid).isAvailable()) {
	            SendResponse status = xmpp.sendMessage(msg);
	            messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
	        }

	        if (!messageSent) {
	            // Send an email message instead...
	        }
	}
	
	public static boolean invite(String id){
		JID jid = new JID(id);
		JID from = new JID("51-job@appspot.com");
       XMPPService xmpp = XMPPServiceFactory.getXMPPService();
       xmpp.sendInvitation(jid, from);
       return true;
	}
}
